/**
 * Copyright ©2003~2018年11月23日 szkingdom
 * @author raohj
 * @date 2018年11月23日
 * @file RThreadTest.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2018年11月23日 由 raohj 新建文件
 */
package com.ccnu.demojava.concurrent.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * @author raohj
 * @date 2018年11月23日
 * @version 
 * @comment to add your comment
 */
public class RThreadTest {
	private static final Logger log = LoggerFactory.getLogger(RThreadTest.class);
	// 测试线程的唤起
	public class RRunnablePrint extends Thread{
		@Override
		public void run() {
			int i = 0;
			while(i++ < 10) {
				try {
					RRunnablePrint.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				log.info("[" + Thread.currentThread().threadId() + "]" + " count:" + i);
			}
			log.info("[" + Thread.currentThread().threadId() + "]" + " return success");
		}
	}
	
	/**
	 * @author raohj
	 * @date 2019年4月8日
	 * @comment 测试线程的同步
	 * @param 
	 * @return 
	 */
	public static void testNotify() {
		Object obj = new Object();
		ArrayList<RThreadWait> rwGroup = new ArrayList<RThreadWait>();
		for(int i=0; i<10; i++) {
			RThreadWait rt = new RThreadWait();
			rt.setMutex(obj);
			rwGroup.add(rt);
			rt.start();
		}
		
		boolean exit = false;
		try {
			while (!exit) {
				for (RThreadWait rt : rwGroup) {
					// 如果检测到线程状态变为等待等待中，则重新启动线程
					if(rt.getState() == Thread.State.WAITING) {
	            		synchronized (obj) {
	                		obj.notify();
	            		}
					}
				}
				
				// 准备退出咯
				exit = true;
				for (RThreadWait rt : rwGroup) {
					if(rt.isAlive()) {
						exit = false;
						break;
					}
				}
				Thread.sleep(10);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		finally {
			System.out.println("[" +Thread.currentThread().threadId() + "]"+"main thread is exit");
		}
	}
	
	/**
	 * @author raohj
	 * @date 2019年4月8日
	 * @comment 测试线程的同步
	 * @param 
	 * @return 
	 */
	public static void testNotifyAll() {
		Object obj = new Object();
		ArrayList<RThreadWait> rwGroup = new ArrayList<RThreadWait>();
		for(int i=0; i<10; i++) {
			RThreadWait rt = new RThreadWait();
			rt.setMutex(obj);
			rwGroup.add(rt);
			rt.start();
		}
		
		boolean exit = false;
		boolean notifyAllFlag = true;
		try {
			while (!exit) {
				// 准备通知所有线程
				notifyAllFlag = true;
				for (RThreadWait rt : rwGroup) {
					// 如果检测到线程状态变为等待等待中，则重新启动线程
					if(rt.getState() != Thread.State.WAITING) {
						notifyAllFlag = false;
						break;
					}
				}
				if(notifyAllFlag) {
					synchronized (obj) {
						obj.notifyAll();
					}
				}
				
				// 准备退出咯
				exit = true;
				for (RThreadWait rt : rwGroup) {
					if(rt.isAlive()) {
						exit = false;
						break;
					}
				}
				Thread.sleep(10);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		finally {
			log.info("[" +Thread.currentThread().threadId() + "]"+"main thread is exit");
		}
	}
	
	// 测试Daemon 后台线程
	public static void testDaemon() {
		RRunnablePrint rt = new RThreadTest().new RRunnablePrint();
		rt.setDaemon(true);
		rt.start();
		
		RRunnablePrint rt1 = new RThreadTest().new RRunnablePrint();
		rt1.setDaemon(false);
		rt1.start();
		
		System.out.println("[" +Thread.currentThread().threadId() + "]"+"main thread is exit");
		return;
	}
	
	// 测试sync
	public static void testSync() {
		RThreadSync.RThreadSyncMethod rsm = new RThreadSync.RThreadSyncMethod();
		RThreadSync rsynct1 = new RThreadSync(rsm);
		RThreadSync rsynct2 = new RThreadSync(rsm);
		RThreadSync rsynct3 = new RThreadSync(rsm);
		
		rsynct1.start();
		rsynct2.start();
		rsynct3.start();
		
		try {
			rsynct1.join();
			rsynct2.join();
			rsynct3.join();
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}
